﻿using ASmile.Com;
using ASmile.DBModel;
using System;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmGridCombox : FrmBase
    {
        public FrmGridCombox()
        {
            InitializeComponent();
        }

        IGridComboxBLL BLLObj { get; } = ObjectFactory.GetObject<IGridComboxBLL>("GridComboxBLL");

        //当前选中的主记录对象
        Sys_GridCombox _CurrMain;

        GridComboxArg SearchArg { get; } = new GridComboxArg();

        void BindMainData()
        {
            SetPageData(1, ucPage.OnePageCount, out int total);
            ucPage.InitiControl(total);
            _CurrMain = null;
            BindColumnData();
        }

        void InitControl()
        {
            ucPage.PageChangedEvent += SetPageData;
            dgvMain.SelectionChangedAndCellClick += DgvMain_SelectionChanged;
        }

        private void DgvMain_SelectionChanged(object sender, DataGridViewCellEventArgs e)
        {
            _CurrMain = dgvMain.GetSelectedClassData<Sys_GridCombox>();
            BindColumnData();
        }

        private void SetPageData(int pageNo, int pageSize, out int total)
        {
            SearchArg.PageNo = pageNo;
            SearchArg.PageSize = pageSize;
            SearchArg.SearchVal = tsTxtSearch.Text;
            var ret = BLLObj.GetMainData(SearchArg);
            total = ret.count;
            dgvMain.DataSource = ret.data;
        }

        private void FrmGridCombox_Load(object sender, EventArgs e)
        {
            InitControl();

            if (Owner is IFormContainer dgvContainer)
            {
                SearchArg.FixedFormName = dgvContainer.ContainerName;

                BindMainData();
            }

            SetCopyInsertSql<Sys_GridCombox>(dgvMain, Keys.P);
            SetCopyInsertSql<Sys_GridCombox_Column>(dgvColumn, Keys.O);
        }

        private void tsBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            SearchArg.ClearADList();
            BindMainData();
        }

        private void tsTxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tsBtnSearch_Click(sender, e);
            }
        }

        private void tsAdSearch_Click(object sender, EventArgs e)
        {
            ADSearch(SearchArg, BindMainData, dgvMain);
        }

        FrmGridComboxEdit CreateEditForm()
        {
            FrmGridComboxEdit frm = new FrmGridComboxEdit();
            frm.FixedFormName = SearchArg.FixedFormName;
            return frm;
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            FrmGridComboxEdit frm = CreateEditForm();
            frm.FixedFormName = SearchArg.FixedFormName;
            var saveModel = new Sys_GridCombox();
            var arg = new EditFormArgs
            {
                ClearId = true,
                SaveHandle = (m) => BLLObj.AddMain(m as Sys_GridCombox),
                SaveModel = saveModel
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                //BindMainData();
                saveModel.Id = ret.data;
                dgvMain.AddDataSource(saveModel);
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnCopyAdd_Click(object sender, EventArgs e)
        {
            var model = dgvMain.GetSelectedClassData<Sys_GridCombox>();
            if (model == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            FrmGridComboxEdit frm = CreateEditForm();
            frm.FixedFormName = SearchArg.FixedFormName;
            var saveModel = new Sys_GridCombox();
            var arg = new EditFormArgs
            {
                ClearId = true,
                SaveHandle = (m) => BLLObj.AddMain(m as Sys_GridCombox),
                SaveModel = saveModel,
                SetModel = model
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                //BindMainData();
                saveModel.Id = ret.data;
                dgvMain.AddDataSource(saveModel);
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnEdit_Click(object sender, EventArgs e)
        {
            var model = dgvMain.GetSelectedClassData<Sys_GridCombox>();
            if (model == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            FrmGridComboxEdit frm = CreateEditForm();
            frm.FixedFormName = SearchArg.FixedFormName;
            var arg = new EditFormArgs
            {
                SaveHandle = (m) => BLLObj.EditMain(m as Sys_GridCombox),
                SaveModel = model,
                SetModel = model
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                BindMainData();
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            if (!ShowQuestion("确定要删除吗？", "删除确认"))
            {
                return;
            }
            var model = dgvMain.GetSelectedClassData<Sys_GridCombox>();
            if (model == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            var ret = BLLObj.DelMain(model);
            ShowMessage(ret.msg);
            if (ret)
            {
                BindMainData();
            }
        }

        void BindColumnData()
        {
            if (_CurrMain == null)
            {
                dgvColumn.DataSource = null;
                return;
            }
            var ret = BLLObj.GetColumnData(_CurrMain.Id);
            dgvColumn.DataSource = ret;
        }

        private void tsBtnSearchColumn_Click(object sender, EventArgs e)
        {
            BindColumnData();
        }

        void AddColumn(bool isCopy)
        {
            if (_CurrMain == null)
            {
                ShowMessage("未选中绑定信息");
                return;
            }
            var saveModel = new Sys_GridCombox_Column { MainId = _CurrMain.Id };
            var editArg = new EditFormArgs
            {
                ClearId = true,
                SaveHandle = (m) => BLLObj.AddColumn(m as Sys_GridCombox_Column),
                SaveModel = saveModel
            };

            if (isCopy)
            {
                editArg.SetModel = dgvColumn.GetSelectedClassData<Sys_GridCombox_Column>();
                if (editArg.SetModel == null)
                {
                    ShowMessage("没有选中记录");
                    return;
                }
            }

            FrmGridComboxColumnEdit frm = new FrmGridComboxColumnEdit();
            frm.DgvRowCount = dgvColumn.RowCount;

            var ret = frm.ShowDialog(editArg);
            if (ret)
            {
                //BindColumnData();
                saveModel.Id = ret.data;
                dgvColumn.AddDataSource(saveModel);
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnAddColumn_Click(object sender, EventArgs e)
        {
            AddColumn(false);
        }

        private void tsBtnCopyAddColumn_Click(object sender, EventArgs e)
        {
            AddColumn(true);
        }

        private void tsBtnEditColumn_Click(object sender, EventArgs e)
        {
            var model = dgvColumn.GetSelectedClassData<Sys_GridCombox_Column>();
            if (model == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            var arg = new EditFormArgs
            {
                SaveHandle = (m) => BLLObj.EditColumn(m as Sys_GridCombox_Column),
                SaveModel = model,
                SetModel = model
            };
            FrmGridComboxColumnEdit frm = new FrmGridComboxColumnEdit();
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                //BindColumnData();
                dgvColumn.Refresh();
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnDeleteColumn_Click(object sender, EventArgs e)
        {
            if (!ShowQuestion("确定要删除吗？", "删除确认"))
            {
                return;
            }
            var model = dgvColumn.GetSelectedClassData<Sys_GridCombox_Column>();
            if (model == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            var ret = BLLObj.DelColumn(model);
            ShowMessage(ret.msg);
            if (ret)
            {
                dgvColumn.DelDataSource(model);
                //BindColumnData();
            }
        }
    }
}